package com.century.sjt.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.entity.ServiceData;

import java.util.List;

/**
 * Created by pig33 on 15-06-19.
 */
public class ServiceListViewAdapter extends BaseAdapter {

    private List<ServiceData> dataList;
    private LayoutInflater mInflater;
    private RequestQueue mQueue;
    private Context mContext;
    private Handler mHandler;



    public ServiceListViewAdapter(Context context, RequestQueue mQueue, List<ServiceData> dataList,Handler mHandler) {
        mInflater = LayoutInflater.from(context);
        this.mQueue = mQueue;
        this.dataList = dataList;
        this.mContext = context;
        this.mHandler = mHandler;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {

        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.home_item_tab2, null);
            holder = new ViewHolder();
            holder.newsTitle = (TextView) convertView.findViewById(R.id.tv_home_title_text);
            holder.layout_item_show = (LinearLayout) convertView.findViewById(R.id.layout_item_show);
            holder.ImgShow = (ImageView) convertView.findViewById(R.id.iv_home_tab1_img);
            holder.iv_item_shows = (ImageView) convertView.findViewById(R.id.iv_item_shows);
            holder.tvForm = (TextView) convertView.findViewById(R.id.tv_home_form_text);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tv_home_tab_text);
            holder.layout_itme_date = (LinearLayout) convertView.findViewById(R.id.layout_itme_date);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.newsTitle.setText(dataList.get(position).getTitle());
        holder.tvContent.setText(dataList.get(position).getContent());
//        Log.e("img",Constant.BaseWebHost + dataList.get(position).getImage());
//        LruImageCache lruImageCache = LruImageCache.instance();
//        ImageLoader mImageLoader = new ImageLoader(mQueue, lruImageCache);
//        img.setImageUrl(Constant.BaseWebHost + image, mImageLoader);

//        holder.iv_item_shows.setImageResource(R.drawable.lower);
//       final View toolBar = (LinearLayout) convertView.findViewById(R.id.layout_itme_date);
        holder.layout_item_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(dataList.get(position).isFalg())){
//                    holder.layout_itme_date.setVisibility(View.GONE);
                    dataList.get(position).setFalg(true);
                    holder.iv_item_shows.setImageResource(R.drawable.upper);
                    dataList.get(position).setIsShow("1");
                }else{
//                    holder.layout_itme_date.setVisibility(View.VISIBLE);
                    dataList.get(position).setFalg(false);
                    holder.iv_item_shows.setImageResource(R.drawable.lower);
                    dataList.get(position).setIsShow("0");
                }
                Message msg = new Message();
                msg.what = 1;
                mHandler.sendMessage(msg);


            }
        });

        holder.layout_itme_date.measure(0, 0);
        if("1".equals(dataList.get(position).getIsShow())){
            ((LinearLayout.LayoutParams)holder.layout_itme_date.getLayoutParams()).bottomMargin = 0;
            holder.layout_itme_date.setVisibility(View.VISIBLE);
        }else{
            ((LinearLayout.LayoutParams)holder.layout_itme_date.getLayoutParams()).bottomMargin = (-1) * holder.layout_itme_date.getMeasuredHeight();
            holder.layout_itme_date.setVisibility(View.GONE);
        }
        Glide.with(mContext).load(dataList.get(position).getImage()).into(holder.ImgShow);
        return convertView;
    }

    private static class ViewHolder {
        private TextView newsTitle;
        private LinearLayout layout_item_show;
        private LinearLayout layout_itme_date;
        private ImageView ImgShow;
        private ImageView iv_item_shows;
        private TextView tvForm;
        private TextView tvContent;

    }
}
